<?php

function sortedDirectoryList($dir, $pattern) {
	
	$files = array();
	
	if ($handle = opendir($dir)) {
		while (false !== ($entry = readdir($handle))) {
			if (preg_match($pattern, $entry)) {
				// sort
				$ctime = filectime($dir . $entry);
	
				for ($i = 0; $i < count($files); ++$i) {
					if ($ctime > $files[$i]["ctime"]) {
						break;
					}
				}
	
				$newent = array('name' => $entry, 'ctime' => $ctime);
				array_splice($files, $i, 0, array($newent));
			}
		}
	
		closedir($handle);
	}
	
	return $files;
}

?>